package org.yy.filter;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

import java.util.Date;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.concurrent.ConcurrentHashMap;

@Component
public class IPBlackFilter implements GlobalFilter, Ordered {
   private static ConcurrentHashMap<String, LinkedList<Long>> map = new ConcurrentHashMap<String, LinkedList<Long>>();


    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();

        String clientIp = request.getRemoteAddress().getHostString();
        String path = request.getURI().getPath();

        if (path.startsWith("/api/user/register") || path.startsWith("/api/code")) {
            System.out.println("过滤路径");
            Date date = DateUtils.addMinutes(new Date(), -1);
            long time = date.getTime();
            LinkedList<Long> times = map.get(clientIp);
            System.out.println("times=" + times);
            int count = 0;
            if (times != null) {
                times.add(System.currentTimeMillis());
                map.put(clientIp, times);
                for (Long t : times) {
                    if (t > time) {
                        count++;
                    }
                }
            } else {
                System.out.println("新list");
                LinkedList<Long> longs = new LinkedList<>();
                longs.add(System.currentTimeMillis());
                map.put(clientIp, longs);
            }
            System.out.println("count=" + count);
            if (count > 10) {
                // 状态码-303
                response.setStatusCode(HttpStatus.SEE_OTHER);
                String data = "您频繁进⾏注册，请求已被拒绝！";
                DataBuffer wrap = response.bufferFactory().wrap(data.getBytes());
                return response.writeWith(Mono.just(wrap));
            }
        }


//        if (clientIp.equals("0:0:0:0:0:0:0:1")) {
//            response.setStatusCode(HttpStatus.UNAUTHORIZED);
//            String data = clientIp + "被拦截";
//            DataBuffer wrap = response.bufferFactory().wrap(data.getBytes());
//            System.out.println(data);
//            return response.writeWith(Mono.just(wrap));
//        }
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
